package com.gerfmarquez.nymph.ui.listeners;

import com.gerfmarquez.nymph.NymphMelodyCreatorActivity;
import com.gerfmarquez.nymph.db.sqlite.DatabaseHelper;
import com.gerfmarquez.nymph.db.sqlite.NoteLengthDbAdapter;
import com.gerfmarquez.nymph.db.sqlite.SongDbAdapter;
import com.gerfmarquez.nymph.db.sqlite.SongNoteLengthDbAdapter;

import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.widget.EditText;

public class OnSaveAsMelodyListener implements DialogInterface.OnClickListener {
	public EditText editText;
	public NymphMelodyCreatorActivity nymph;
	
	public OnSaveAsMelodyListener(NymphMelodyCreatorActivity nymph) {
		this.nymph = nymph;
	}
	@Override
	public void onClick(DialogInterface arg0, int arg1) {
		// TODO Auto-generated method stub
		saveSong(editText.getText().toString());
	}
    public void saveSong(String songName) {
    	DatabaseHelper dbHelper = new DatabaseHelper(nymph);
    	SQLiteDatabase database =dbHelper.getWritableDatabase();
    	
    	SongDbAdapter songAdapter = new SongDbAdapter(database);
		SongNoteLengthDbAdapter sNLDA = new SongNoteLengthDbAdapter(database);

		
		long idSong = songAdapter.createSong(songName, 1);
		int[] notes = nymph.getNotesFromSlideViews();
		byte[] rhythms = nymph.getNotesRhythm();
    	
		for(int i = 0; i < notes.length; i++ ) {
			NoteLengthDbAdapter noteLengthAdapter = new NoteLengthDbAdapter(database);
			int idNoteLength = noteLengthAdapter.getNoteLengthId((int)rhythms[i]);
			sNLDA.createSongNoteLength(idSong, notes[i],idNoteLength);
		}
    	
    	dbHelper.close();
    }
}


